﻿package ascript.components.collision
{
	//#########################
	// Header Includes
	//#########################
	import ascript.Game;
	import ascript.GameObject;
	import Box2D.Dynamics.b2Body;
	import Box2D.Dynamics.b2Fixture;
	
	//#########################
	// GeneralModifiers Class
	//#########################
	public class CollisionAIPatrol extends CollisionBase
	{
		//=========================
		// Constructors
		//=========================	
		public function CollisionAIPatrol():void
		{
		}
		
		//=========================
		// Members
		//=========================
		override public function initComponent( tGame:Game, tObject:GameObject ):void
		{
			// Inheritance
			super.initComponent( tGame, tObject );
			
			// Set Default Attributes
			defaultAttribute( tObject, "PatrolTargetID", new Vector.<String> );
			defaultAttribute( tObject, "PatrolTargetFoundID", new Vector.<String> );
			defaultAttribute( tObject, "PatrolFixtureID", "Patrol" );
		}
		
		override public function handleCollision( tObject:GameObject, tObjectHit:GameObject, tFixture:b2Fixture, tFixtureHit:b2Fixture, tIsTouching:Boolean ):Boolean
		{
			var tempTouched:Boolean = false;
					
			// Inheritance
			if ( super.handleCollision( tObject, tObjectHit, tFixture, tFixtureHit, tIsTouching ) && tFixture.GetUserData() == tObject.getAttribute( "PatrolFixtureID" ) )
			{			
				var tempListLength:uint = ( tObject.getAttribute( "PatrolTargetID" ) ).length;
				for ( var i:uint = 0; i < tempListLength; i++ )
				{
					if ( tObject.getAttribute( "PatrolTargetID" )[i] == tObjectHit.getAttribute( "CollisionID" ) )
					{
						if ( tIsTouching )
						{
							( tObject.getAttribute( "PatrolTargetFoundID" ) ).push( tObjectHit.getName() );
						}
						else
						{
							( tObject.getAttribute( "PatrolTargetFoundID" ) ).splice( ( tObject.getAttribute( "PatrolTargetFoundID" ) ).indexOf( tObjectHit.getName() ), 1 );
						}
						
						tempTouched = true;
					}
				}
				
				if ( tempTouched )
				{
					if ( tIsTouching && !tObject.getAttribute( "HomingIsActive" ) )
					{
						tObject.setAttribute( "HomingTarget", tObject.getAttribute( "PatrolTargetFoundID" )[ Math.floor( Math.random() * ( tObject.getAttribute( "PatrolTargetFoundID" ) ).length ) ] ); 
						tObject.setAttribute( "HomingIsActive", true );
						tObject.setAttribute( "WaypointIsActive", false );
					}
					else if ( !tIsTouching && !tObject.getAttribute( "WaypointIsActive" ) )
					{
						tObject.setAttribute( "HomingIsActive", false );
						tObject.setAttribute( "WaypointIsActive", true );
					}
				}
			}
			
			return tempTouched;
		}
		
		override public function solveCollision( tObject:GameObject, tObjectHit:GameObject, tFixture:b2Fixture, tFixtureHit:b2Fixture, tIsPresolve:Boolean ):Boolean
		{
			// DONT CARE
			return false;
		}
	}
}